#!/bin/bash
### BEGIN INIT INFO
# Provides:             facelog
# Required-Start:       $syslog $remote_fs
# Required-Stop:        $syslog $remote_fs
# Should-Start:         $local_fs
# Should-Stop:          $local_fs
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    facelog service
# Description:          facelog service
### END INIT INFO
# 启动 facelog 服务
# 在 service 管理系统服务的平台要修改service_user和service_folder
# 在 systemctl 管理系统服务的平台,不要直接使用此脚本控制服务启动，
# 应该创建 /etc/systemd/system/facelog.service,在 facelog.service 中调用此脚本，此脚本不需要修改

# 指定运行脚本的帐户名,用于 service 管理系统服务时要修改此变量为指定启动的服务的帐户
service_user=$(whoami)
# 指定服务工作文件夹,用于 service 管理系统服务时要修改此变量为固定常量
service_folder=$(dirname $(readlink -f $0))
# 指定运行脚本日志输出文件
service_out='facelog.out'
[ "$service_user" != "$(whoami)" ] && [ "$(whoami)" != 'root' ] && echo "必须在 $service_user 或 root 用户下执行" && exit 255

config="/home/$service_user/.facelog/config.properties"
function prop {
	[ -f "$config" ] && grep -P "^\s*[^#]?${1}=.*$" $config | cut -d'=' -f2
}
## 读取properties中的参数，并创建参数名命名的变量，如果参数名中包含'.'则替换为'-'
if [ -f "$config" ]
then
	#echo "load property from $config"
	while IFS='=' read -r key value || [ -n "$key" ]
	do
		[ -z "$key" ] && continue
		
		# '.'替换为'-'
		key=$(echo $key | tr '.' '_')
		## 不处理＃起始的key
		[[ -z $(echo "$key" | grep -E '\s*#+.*' ) ]] \
			&& eval "${key}='${value}'"
	done < "$config"
else
	echo "$config not found."
fi

[ -z "$server_port" ] && server_port=26411

# 返回 facelog 的 PID
function facelog_pid(){
	ps -ef | grep -v grep | grep -E "java\s+.*-jar\s+\S*facelog-service-\S+" | awk '{print $2}'
}
# 判断指定的PID 占用的端口是否为$server_port,是返回0,否则返回-1
# $1 进程ID
function is_server_port(){
	netstat -nlp 2>/dev/null | grep "$1" | grep :$server_port 
}
## 停止 facelog
function stop(){
	local pid=$(facelog_pid)
	if [ -n "$pid" ] 
	then	
		kill -9 $pid || exit 255 
		echo "facelog $pid killed"
	fi
	return 0
}
## 启动 facelog
function start(){

	local pid=$(facelog_pid)
	if [ -n "$pid" ] 
	then
		is_server_port $pid && echo "服务已上线" && return 0
	else	
		nc -w 1 localhost $server_port 2>/dev/null && echo "ERROR:服务端口被占用(facelog port $server_port occupied by other process)" && return 255
	fi
	# 设置 HOME 位置	
	[ "$service_user" != "$(whoami)" ] && export FACELOG_OPTS="-Duser.home=/home/$service_user"
	# 设置 java 搜索路径
	[ -n "$java_exe" ] && [ "$(which java 2>/dev/null)" != "$java_exe"] && export PATH="$java_exe":$PATH
	# 是否挂起
	local no_hang_up=0;
	# 是否输出重定向
	local redirect=" > $service_out 2>&1";
	# 解析输入参数
	while [ $# -ge 1 ] ; do
		case "$1" in
			-u|--hup) no_hang_up=1; shift 1;;
			-s|--stdout) redirect='' ; shift 1;;
			*) echo "无效参数 $1." ; exit 255;;
		esac
	done
	echo "启动 facelog 服务(start facelog service)..."
	[ -n "$redirect" ] && echo "执行 tail -f $service_folder/facelog.out 查看控制台输出"

	if [ $no_hang_up -eq 0 ] 
	then		
		if [ "$service_user" != "$(whoami)" ]
		then
			su -c "cd $service_folder; nohup ./facelog-service/target/start_facelog_server.sh $redirect &" - "$service_user"
		else
			eval nohup ./facelog-service/target/start_facelog_server.sh $redirect &
		fi
	else
		# --hup 用于 systemctl 控制
		eval ./facelog-service/target/start_facelog_server.sh $redirect
	fi
}

## 显示 facelog 服务状态
function status(){
	# 当前运行的 facelog PID
	local pid=$(facelog_pid)
	# 检测 facelog 是否正常运行
	[ -n "$pid" ] && echo "facelog    [ON] PID $pid PORT $server_port " && return 0
	echo "facelog    [OFF]"
	return 1
}
pushd $service_folder > /dev/null 2>&1
case $1 in
	start)
		shift 1;
		start $*
		exit 0
		;;
	stop)
		stop
		exit 0
		;;
	status)
		status				
		exit 0
		;;
	restart|force-reload)
		stop
		start
		exit 0
		;;
	*)
		echo "Usage: /etc/init.d/facelog {start|stop|restart|force-reload|status}"
		exit 1
		;;
esac
popd > /dev/null 2>&1
